package com.qinglei.recoup.system.task;

import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinglei.recoup.hmp.pojo.event.SendMpHealthReportEvent;
import com.qinglei.recoup.system.domain.RadarWechat;
import com.qinglei.recoup.system.domain.UserWechat;
import com.qinglei.recoup.system.service.RadarWechatService;
import com.qinglei.recoup.system.service.UserWechatService;
import com.qinglei.recoup.system.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class WxTask {

    @Autowired
    private RadarWechatService radarWechatService;

    @Autowired
    private UserWechatService  userWechatService;

    @Autowired
    private WxService wxService;

    @Autowired
    private ApplicationContext applicationContext;


    @Value("${wx.appId}")
    private  String APPID ;

    @Value("${wx.appSecret}")
    private  String SECRET;

    @Value("${wx.threadCount}")
    private  String threadCount;

    @Value("${wx.isSendMessage}")
    private  boolean  isSendMessage;


    private Executor executors = Executors.newFixedThreadPool(2);

    public void sendReport(){
        if(isSendMessage){
            log.info("____________________startsendReport___________________________");
            QueryWrapper queryWrapperRadarWechat = new QueryWrapper();
            queryWrapperRadarWechat.eq("is_subscribe","1") ;
            queryWrapperRadarWechat.eq("is_deleted","0");
            List<RadarWechat> userWechatList =  radarWechatService.list(queryWrapperRadarWechat);
            for(RadarWechat radarWechatTmp : userWechatList){
                QueryWrapper queryWrapperUserWechat = new QueryWrapper();
                queryWrapperUserWechat.eq("id",radarWechatTmp.getUserWechatId());
                queryWrapperUserWechat.eq("is_deleted","0");
                UserWechat userWechat  = userWechatService.getOne(queryWrapperUserWechat);
                if(userWechat!=null){
                    String openId =  userWechat.getOpenId();
                    String snCode =radarWechatTmp.getSnCode();
                    String nickName = radarWechatTmp.getNickName();
                    CompletableFuture.runAsync(() -> {
                        try {
                            sendMessage(openId,snCode,nickName);
                        } catch (Exception e) {
                            log.error("sendMessage wx error", e);
                        }
                    }, executors);

                }
            }
            // 给清雷健康小程序用户发送报告到清雷健康微信公众号
            SendMpHealthReportEvent sendMpHealthReportEvent = new SendMpHealthReportEvent(applicationContext);
            sendMpHealthReportEvent.setToday(LocalDate.now());
            applicationContext.publishEvent(sendMpHealthReportEvent);
            log.info("sendReport sendMpHealthReportEvent:{}", sendMpHealthReportEvent);

            log.info("____________________endsendWxMessage___________________________");
        }
    }

    private void sendMessage(String openId,String snCode,String nickName){
           wxService.send_template_message(APPID,openId,snCode,nickName);
           wxService.sendTemplateMessageFirstDayOfMonth(APPID,openId,snCode,nickName);
           wxService.sendTemplateMessageFirstDayOfWeek(APPID,openId,snCode,nickName);
    }


}
